home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!ns.unibol.com
- From: John Girvin <jgirvin@bfs.unibol.com>
- Newsgroups: comp.sys.amiga.programmer
- Subject: Re: Collision Detects !!!!
- Date: Fri, 5 Apr 1996 16:01:03 GMT
- Message-ID: <199604051601.QAA18722@mailhost.unibol.com>
- X-NNTP-Posting-Host: ns.unibol.com
- X-Newsreader: TIN [version 1.2 PL2]
- X-Mail2News-Path: ns.unibol.com
-
- On Thu, 4 Apr 1996 11:52:56 GMT, breese@imada.ou.dk (Bjorn Reese) wrote:
- :>John Girvin (jgirvin@bfs.unibol.com) wrote:
- :>> px2 = player->x + player->width // PreCalc player BRHC coords.
- :>> py2 = player->y + player->height
- :>>
- :>> for (i=1; i<num_bullets; i++)
- :>> {
- :>> if ( (bullet[i]->x - px2) < (bullet[i]->width + player->width) &&
- :>> (bullet[i]->y - py2) < (bullet[i]->height + player->height) )
-
- :>I might be missing something, but it seems to me that this only works
- :>under the assumption that the bullets are to the right and above the
- :>player (ie. bullet[i]->x < player->x and similar for y.)
-
- No, Im just missing a decent code-recall system in my brain :)
- This code is plain wrong! My faulty memory made me switch the
- order of the subtraction *blush* It should read:
-
- if ( (px2 - bullet[i]->x) < (bullet[i]->width + player->width) &&
- ^^^ ^^^^^^^^^^^^
- swapped!
- (py2 - bullet[i]->y) < (bullet[i]->height + player->height) )
- ^^^ ^^^^^^^^^^^^
- swapped!
-
- Remember that these are *UNSIGNED* comparisons, thats the "trick".
-
- Time for a few diagrams; Ill just do the X comparisons since Y is
- essentially the same. "P" is the player and "B" is the bullet:
-
- 1) Bullet too far left:
- +-----+
- | |
- | P |
- | |
- +-----+<-px2
-
- |<-------->| (this distance is px2-bx)
-
- bx-> +-+
- |B|
- +-+
-
- Think of (px2-bx) as the x-distance between the corners of the bounding
- rectangles. If this distance is further than the combined widths, then
- the rectangles are too far apart.
-
- 2) Collision!
- +-----+
- | |
- | P |
- | |
- +-----+<-px2
-
- |<---->| (this distance is px2-bx [which is < pw+bw])
-
- bx-> +-+
- |B|
- +-+
-
- Imagine sliding the "B" rectangle in diagram 1 above to the right (the
- bullet moves closer to the player). px2-bx will become smaller since
- bx is increasing. Eventually the distance from x=px2 to x=bx will become
- small enough so that the rectangles will overlap and you have a collision.
-
- 3) Bullet too far right:
-
- +-----+
- | |
- | P |
- | |
- +-----+<-px2
-
- |<->| (this distance is px2-bx)
-
- bx-> +-+
- |B|
- +-+
-
- This is the tricky one :) Here, bx>px2 (bx is further to the right than
- px2) so (px2-bx) is less than 0, right?
- Wrong!
-
- We are using *unsigned* arithmetic so (px2-bx) is actually a Very Big
- Number, eg: -8 = 0xfff8 > 65000 , so the expression
- (px2 - bullet[i]->x) < (bullet[i]->width + player->width)
- is false and the check terminates.
-
- :>as player->width is present on both sides of the inequality it can be
- :>omitted.
- Well spotted! I never noticed that myself :) I dont think it would make
- a big difference if bullet->width and bullet->height were constants. Youd
- save 2 adds outside the loop and have different constants inside.
-
- :>The simple "check all ordered pair of entities" approach will
- :>always win over more sophisticated approaches if the number of
- :>entities are small (without specifying how small "small" is.)
-
- Yes, I discovered this while experimenting to find the "best"
- collision detection method for my purposes! But it always comes
- down to comparing two entities, so if you optimize that check
- you always gain no matter how complex the scheme built on top.
- (c) Obvious Statements Inc. ;)
-
- [static subsection collision checking]
- :>I never really liked this idea of subsections for two reasons.
- :> (1) Problems with boundaries. To solve this you have to check
- :> the neighbouring subsections as well.
- Not necessarily, I think. I read one document that claimed to solve
- this problem by shifting the sector dividing lines by half a sector
- width&height and repeating the collision check 4 times in all for
- :x,y x+0.5,y x,y+0.5 x+0.5,y+0.5. It was something like that anyway,
- but dont quote me - youve seen what my memory is like already ;)
-
- I have the paper at home. Mail me at girv@girvnet.demon.co.uk if you
- (or anyone else!) want it, or actual in-use 68k code to demonstrate
- the 2-comparison collision check.
-
- :>Subsections (static partitioning) can be sketched by this
- :> for previous, current & next y-subsection
- :> for previous, current & next x-subsection
- :> CheckCollision
- 9 * CheckCollision for every sector = 0.5fps if youre lucky ;)
- The paper I was on about does 4 per sector I think. Much better!
-
- cya, and sorry for any confusion!
- /John.
- __________________________________________________________________________
- |/\John Girvin : developing software for Unibol Inc., speaking for myself|
- |\/jgirvin@bfs.unibol.com | Amiga,!PC,net,Trek,SF,MTB,C2H50H,house,techno|
- |girv@girvnet.demon.co.uk | Youll never take me alive, Macro$loth fiends!|
- \A1200/030-40/10M/3.0 A500/000-7/2M/2.04 464/Z80-4/0.0625M/1.0 Team AMIGA/
-